﻿// -------------------------------------------------------------------------
//    @FileName         :    AuthAction.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-1
//    @Email            :    445267987@qq.com
//    @Module           :    AuthAction
//
// -------------------------------------------------------------------------

#include "AuthAction.h"

#include <NFComm/NFPluginModule/NFCheck.h>
#include "GameApi.h"
#include "ProtoUtil.h"

int AuthSynAction::MakePacket()
{
	CHECK_NULL(0, m_pGameAPI);

	return ProtoUtil::MakeAuthSyn(*m_pGameAPI, m_pGameAPI->m_iUid);
}

int AuthSynAction::HandlePacket(proto_client2access::C2AMsgHead& oMsgHead, char* pData, int iDataLen, bool& bIsEnd)
{
	bIsEnd = false;
	switch (oMsgHead.cmd())
	{
		case proto_client2access::E_A2C_AUTH_SYNACK:
		{
			bIsEnd = true;

			proto_client2access::A2C_AuthSynAck syn_ack;
			syn_ack.ParseFromArray(pData, iDataLen);

			bool bRet = m_pGameAPI->init_aes(syn_ack.dhpubkey_server().c_str(), syn_ack.dhpubkey_server().size());
			if (bRet != true)
			{
				NFLogError(NF_LOG_DEFAULT, 0, "init aes failed!");
				bIsEnd = false;
				return -1;
			}

			m_pGameAPI->m_compress_type = syn_ack.compress();
			m_pGameAPI->m_encrypt_type  = syn_ack.encrypt();
			break;
		}
		default:
		{
			NFLogError(NF_LOG_DEFAULT, 0, "invalid cmd:{}", (int)oMsgHead.cmd());
			//return -1;
		}
	}

	return 0;
}

int AuthAckAction::MakePacket()
{
	CHECK_NULL(0, m_pGameAPI);

	return ProtoUtil::MakeAuthAck(*m_pGameAPI, m_pGameAPI->m_iUid);
}

int AuthAckAction::HandlePacket(proto_client2access::C2AMsgHead& oMsgHead, char* pData, int iDataLen, bool& bIsEnd)
{
	bIsEnd = false;
	switch (oMsgHead.cmd())
	{
		case proto_client2access::E_A2C_STARTOK:
		{
			bIsEnd = true;
			break;
		}
		default:
		{
			NFLogError(NF_LOG_DEFAULT, 0, "invalid cmd:{}", (int)oMsgHead.cmd());
			//return -1;
		}
	}

	return 0;
}
